package org.liveboardgames.agricola.control.delegates.mock
{
	import mx.collections.ArrayCollection;
	import mx.collections.ListCollectionView;
	
	import org.liveboardgames.agricola.vo.common.Peep;
	import org.liveboardgames.agricola.vo.farm.Farm;
	import org.liveboardgames.agricola.vo.farm.FarmElement;
	import org.liveboardgames.agricola.vo.farm.FarmTile;
	import org.liveboardgames.agricola.vo.farm.Pasture;
	import org.liveboardgames.agricola.vo.player.Player;

	public class FarmFactory
	{
		public static const NUMBER_OF_TILES:int = 15;

		public static const NUMBER_OF_FENCE_SPACES:int = 38;

		public static function getInitialFarm(player:Player):Farm
		{
			var farm:Farm = getNewFarm();

			// Create the two peeps
			var firstPeep:Peep = new Peep();
			var secondPeep:Peep = new Peep();
			
			player.peeps = new ArrayCollection();
			player.peeps.addItem(firstPeep);
			player.peeps.addItem(secondPeep);

			// Get the two tiles that will host the first elements
			var firstTile:FarmTile = farm.getTileAt(0);
			var secondTile:FarmTile = farm.getTileAt(1);

			// Add the rooms to the tiles
			firstTile.farmElement = FarmElement.WOODEN_ROOM;
			secondTile.farmElement = FarmElement.WOODEN_ROOM;
			//FarmTile(farm.getTileAt(2)).farmElement = new Room(HouseTypeEnum.WOODEN);

			// Add the peeps on the tiles
			firstTile.addPeep(firstPeep);
//			secondTile.addPeep(secondPeep);

//            firstTile.setResource(ResourceTypeEnum.SHEEP, 1);

//            FarmTile(farm.getTileAt(3)).pasture = new Pasture(1);
//            FarmTile(farm.getTileAt(4)).pasture = new Pasture(2);

			return farm;
		}

		private static function getNewFarm():Farm
		{
			// The Farm itself
			var farm:Farm = new Farm();
			// Initialize the tiles
			var tiles:ListCollectionView = new ArrayCollection();
			for (var i:int = 0; i < NUMBER_OF_TILES; i++)
			{
				var farmTile:FarmTile = new FarmTile();
				farmTile.farm = farm;
				farmTile.id = i;
				tiles.addItem(farmTile);
			}
			farm.tiles = tiles;
			return farm;
		}

		public static function getNewPasture(farm:Farm):Pasture
		{
			var maxId:int = 0;

			for each (var tile:FarmTile in farm.tiles)
			{
				if (tile.pasture != null)
				{
					maxId = Math.max(maxId, tile.pasture.id);
				}
			}

			var pasture:Pasture = new Pasture();
			pasture.id = maxId + 1;

			return pasture;
		}
	}
}